package ve.usb;

import DBMS.DataBaseManager;
import java.util.ArrayList;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts.action.*;

import ve.usb.cohesion.runtime.CohesionAction;


/**
 * 
 */
public class AccionesIniciarSesion extends CohesionAction {
    
/**
     * Acci??n llamada por Struts para ejecutar la acci??n A_PreIniciarSesion.
     * 
     * @param mapping The ActionMapping used to select this instance.
     * @param form The optional ActionForm bean for this request.
     * @param request The HTTP Request we are processing.
     * @param response The HTTP Response we are processing.
     * @throws java.lang.Exception
     * @return
     */
    public ActionForward A_PreIniciarSesion(ActionMapping mapping, ActionForm  form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        
        //Salidas
        final String[] SALIDAS = {"V_IniciarSesion","V_HomeDeportista","A_HomeEntrenador", "V_AprobarAsociacionDIrector",};
        final int SALIDA_0 = 0;

        int salida = SALIDA_0;
        /*
            InicioSesion fInicioSesion = (InicioSesion)form;
            request.getSession().setAttribute("Usuario",fInicioSesion.getLogin());
            if (fInicioSesion.getLogin().equals("Estudiante") && fInicioSesion.getPassword().equals("123")){
                
                salida= 1;
            }
            if (fInicioSesion.getLogin().equals("Entrenador") && fInicioSesion.getPassword().equals("123")){
                salida= 2;
            }
            if (fInicioSesion.getLogin().equals("Director") && fInicioSesion.getPassword().equals("123")){
                salida= 3;
            }
            * 
            */
            //System.out.println(fInicioSesion.getLogin());
        
   //     System.out.println(reg);
    
            return mapping.findForward(SALIDAS[salida]);
    }

/**
     * Acci??n llamada por Struts para ejecutar la acci??n A_Conectarse.
     * 
     * @param mapping The ActionMapping used to select this instance.
     * @param form The optional ActionForm bean for this request.
     * @param request The HTTP Request we are processing.
     * @param response The HTTP Response we are processing.
     * @throws java.lang.Exception
     * @return
     */
    public ActionForward A_Conectarse(ActionMapping mapping, ActionForm  form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        
        //Salidas  
        final String[] SALIDAS = {"V_IniciarSesion","V_HomeDeportista","V_HomeEntrenador", "V_HomeDirector","V_IniciarSesion",};
       
        final int SALIDA_0 = 0;
        final int SALIDA_1 = 1;
        int salida = SALIDA_0;
        DataBaseManager dbms = DataBaseManager.getInstance();
         InicioSesion fInicioSesion = (InicioSesion)form;
            request.getSession().setAttribute("Login",fInicioSesion.getLogin());
            if (dbms.estaDeportista(fInicioSesion) && fInicioSesion.getPassword().equals("123")){
                request.getSession().setAttribute("deportista",true);
                salida= 1;
                
            }
            else if (dbms.estaEntrenador(fInicioSesion) && fInicioSesion.getPassword().equals("123")){
                salida= 2;
                request.getSession().setAttribute("entrenador",true);
            }
            else if (dbms.estaDirector(fInicioSesion) && fInicioSesion.getPassword().equals("123")){
                request.getSession().setAttribute("director",true);
                salida= 3;
            }else{
                ActionErrors e = new ActionErrors();
            e.add("errorlogin", new ActionMessage("errorlogin"));
                saveErrors(request, e);
                salida=4;
            }
        return mapping.findForward(SALIDAS[salida]);

    }

/**
     * Acci??n llamada por Struts para ejecutar la acci??n A_Desconectarse.
     * 
     * @param mapping The ActionMapping used to select this instance.
     * @param form The optional ActionForm bean for this request.
     * @param request The HTTP Request we are processing.
     * @param response The HTTP Response we are processing.
     * @throws java.lang.Exception
     * @return
     */
    public ActionForward A_Desconectarse(ActionMapping mapping, ActionForm  form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        
        //Salidas
        final String[] SALIDAS = {"A_PreIniciarSesion", };
        final int SALIDA_0 = 0;

        int salida = SALIDA_0;
        

        return mapping.findForward(SALIDAS[salida]);
    }

/**
     * Acci??n llamada por Struts para ejecutar la acci??n A_PreLogin.
     * 
     * @param mapping The ActionMapping used to select this instance.
     * @param form The optional ActionForm bean for this request.
     * @param request The HTTP Request we are processing.
     * @param response The HTTP Response we are processing.
     * @throws java.lang.Exception
     * @return
     */
    public ActionForward A_PreLogin(ActionMapping mapping, ActionForm  form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        
        //Salidas
        final String[] SALIDAS = {"V_IniciarSesion", };
        final int SALIDA_0 = 0;

        int salida = SALIDA_0;
        

        return mapping.findForward(SALIDAS[salida]);
    }



}